package com.longdaji.decoration.ui.main.home;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.GoodsCategory;
import com.longdaji.decoration.data.bean.GoodsChosensBean;
import com.longdaji.decoration.data.bean.GoodsMotifsBean;
import com.longdaji.decoration.data.bean.ShareUrlBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class HomePresenter extends RxPresenter<HomeContract.View> implements HomeContract.Presenter {
    private final static String TAG = HomePresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public HomePresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void getGoodsCatergory() {
        addSubscribe(mDataManager.getGoodsCatergory()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsCategory>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsCategory>() {

                    @Override
                    public void onSuccess(GoodsCategory goodsCatergory) {
                        Log.d(TAG, "success,goods detail is " + goodsCatergory.toString());
                        if (mView != null) {
                            mView.showCategoryList(goodsCatergory.getSecondCategory());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getTopicImage() {
        addSubscribe(mDataManager.getTopicImage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsMotifsBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsMotifsBean>() {

                    @Override
                    public void onSuccess(GoodsMotifsBean goodsMotifsBean) {
                        Log.d(TAG, "success,getTopicImage is " + goodsMotifsBean);
                        if (mView != null) {
                            if (goodsMotifsBean != null && goodsMotifsBean.getGoodsMotifs() != null) {
                                mView.showTopicImage(goodsMotifsBean.getGoodsMotifs());
                            } else {
                                mView.showTopicImage(new ArrayList<GoodsMotifsBean.GoodsMotifs>());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                           // mView.toast(msg);
                            mView.showTopicImage(new ArrayList<GoodsMotifsBean.GoodsMotifs>());
                        }
                    }
                }));
    }

    @Override
    public void getChosenImages() {
        addSubscribe(mDataManager.getChosenImage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsChosensBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsChosensBean>() {

                    @Override
                    public void onSuccess(GoodsChosensBean goodsChosensBean) {
                        Log.d(TAG, "success,getChosenImages is " + goodsChosensBean.getGoodsChosens().size());
                        if (mView != null) {
                            if (goodsChosensBean != null && goodsChosensBean.getGoodsChosens() != null && goodsChosensBean.getGoodsChosens().size() > 0) {
                                mView.showChosenImages(goodsChosensBean.getGoodsChosens());
                            } else {
                                mView.showChosenImages(new ArrayList<GoodsChosensBean.GoodsChosensImg>());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                            mView.showChosenImages(new ArrayList<GoodsChosensBean.GoodsChosensImg>());
                        }
                    }
                }));
    }

    @Override
    public void getShareUrl() {
        addSubscribe(mDataManager.getShareUrl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<ShareUrlBean>handleResult())
                .subscribeWith(new CommonSubscriber<ShareUrlBean>() {

                    @Override
                    public void onSuccess(ShareUrlBean shareUrlBean) {
                        Log.d(TAG, "success,shareUrlBean is " + shareUrlBean);
                        if(shareUrlBean==null){
                            return;
                        }
                        if (mView != null) {
                            mView.showShareUrl(shareUrlBean.getHref());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                    }
                }));
    }
}
